<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;

class ClientController extends Controller
{
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.client.list', compact('menu_id', 'toolBarButton'));
    }

    public function listData()
    {
        try{
            $list = callApiServer("System.ClientService.getInfoList");
            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
        return view('system.client.form');
    }

    public function showEditForm()
    {
        $params['id'] = request('client_id');
        try{
            $info = callApiServer("System.ClientService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            return view('system.client.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['company_name'] = request('company_name');
        $params['company_address'] = request('company_address');
        $params['company_phone'] = request('company_phone');
        $params['user_prefix'] = request('user_prefix');

        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'账套名称不能为空']);
        }

        $rs = callApiServer("System.ClientService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function remove()
    {
        $params['id'] = request('id');
        $rs = callApiServer("System.ClientService.remove", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>'删除失败']);
    }
}
